import { NextRequest, NextResponse } from "next/server";
import qs from "query-string";

export async function GET(request: NextRequest) {
  // 构建目标URL
  const queryString = qs.parseUrl(request.nextUrl.toString());

  // 发起转发请求
  const response = await fetch(
    qs.stringifyUrl({
      url: "http://127.0.0.1:8188/view",
      query: queryString.query,
    }),
  );

  // 返回响应
  if (response.ok) {
    return await response; // 假设返回的是JSON数据
  } else {
    return NextResponse.json(
      { error: "Failed to fetch data" },
      { status: response.status },
    );
  }
}
// http://127.0.0.1:8188/view?http%3A%2F%2Flocalhost%3A3000%2Fimage%3Ffilename=ComfyUI_00677_.png&subfolder=&type=output
// http://127.0.0.1:8188/view?filename=ComfyUI_00676_.png&subfolder=&type=output
